from templates.config import conn
import datetime,os
cur = conn.cursor()


def show_dbs():
    sql = "show databases"
    cur.execute(sql)
    result = cur.fetchall()
    return result
    # for i in result:
    #     print(i)
def show_tables(cdb):
    sql = "show tables"
    # sql2 = cdb
    cur.execute(cdb)
    cur.execute(sql)
    result = cur.fetchall()
    return result
    # for i in result:
    #     print(i)


def show_conf():
    sql = "SHOW GLOBAL VARIABLES"
    cur.execute(sql)
    result = cur.fetchall()
    return result
# def bak_tables():
#     sql = "show tables"
#     cur.execute(sql)
#     result = cur.fetchall()
#     for table in result:
#         print(table)
#         print(type(table))
#         for i in table:
#             MYSQLDUMP_CMD = "mysqldump + ' -h' + host + ' -u' + user + ' -p' + password + ' ' + database + ' ' + table +' >' + Destination_dir + database + '-' + timestamp + '-' + i + '.sql'"
#             result = os.system(MYSQLDUMP_CMD)
#             return result

#创建db的函数，需要传入db参数值
def newdbs(db):
    #操作数据库的sql语句，{}是参数占位符,通过.format()引入
    sql = "create database {}".format(db)
    #通过数据库游标的execute()方法来执行sql命令
    cur.execute(sql)
    #sql提交事务，完成sql的写操作
    conn.commit()


def deldbs(db):
    sql = "drop database {}".format(db)
    cur.execute(sql)
    conn.commit()

if __name__ == '__main__':
    show_dbs()
    show_tables()